<template>
  <div class="app-container">
<!--    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">-->
<!--      <el-form-item>-->
<!--        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
<!--        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
<!--      </el-form-item>-->
<!--    </el-form>-->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="satisfiedList" @selection-change="handleSelectionChange">
      <el-table-column type="index" label="序" width="30" align="center" />
      <el-table-column label="调查分类" align="center" prop="surveyType" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.survey_type" :value="scope.row.surveyType"/>
        </template>
      </el-table-column>
      <el-table-column label="调查时间" align="center" prop="investigationTime" width="90">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.investigationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="调查人" align="center" prop="investigator" width="70"/>
      <el-table-column label="被调查人" align="center" prop="respondent" width="70"/>
      <el-table-column label="满意度评分" align="center" prop="satisfactionScore" width="100">
<!--        <template slot-scope="scope">-->
<!--          <el-rate v-model="scope.row.satisfactionScore" disabled show-score text-color="#ff9900"/>-->
<!--        </template>-->
      </el-table-column>
      <el-table-column label="纠正预防" align="center" prop="correction" :show-overflow-tooltip="true" width="120"/>
      <el-table-column label="责任人" align="center" prop="responsiblePerson" width="70"/>
      <el-table-column label="关闭时间" align="center" prop="closeTime" width="90">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.closeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="附件" align="center" prop="uploadFile" width="80">
        <template slot-scope="scope">
          <el-button @click.stop="handlePictureCardPreviewResume(scope.row.id)" size="small"
                     type="text">附件</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"       title="编辑"></el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"       title="删除"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户满意度对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" height="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="调查分类" prop="surveyType">
              <el-select v-model="form.surveyType" placeholder="请选择调查分类" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.survey_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="调查时间" prop="investigationTime">
              <el-date-picker clearable
                v-model="form.investigationTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择调查时间"
                style="width: 100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="调查人" prop="investigator">
              <el-input v-model="form.investigator" placeholder="请输入调查人"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="被调查人" prop="respondent">
              <el-input v-model="form.respondent" placeholder="请输入被调查人"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="满意度评分" prop="satisfactionScore">
              <el-input v-model="form.satisfactionScore" placeholder="请输入满意度平均分"  :min="0"/>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="纠正预防" prop="correction">
              <el-input
                type="textarea"
                v-model="form.correction"
                placeholder="请输入纠正预防措施"
                :autosize="{ minRows: 3, maxRows: 5 }"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
                    <el-col :span="12">
                      <el-form-item label="责任人" prop="responsiblePerson">
                        <el-input v-model="form.responsiblePerson" placeholder="请输入责任人"/>
                      </el-form-item>
                    </el-col>
          <el-col :span="12">
            <el-form-item label="关闭时间" prop="closeTime">
              <el-date-picker clearable
                v-model="form.closeTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择关闭时间"
                style="width: 100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
                    <el-col :span="12">
                      <el-form-item label="附件" prop="uploadFile">
                        <file-view v-if="show" :type="typeSelect" :businessId="form.id" :businessCode="businessCode"/>
                      </el-form-item>
                    </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 附件展示对话框 -->
    <el-dialog title="查看附件" :visible.sync="resumeFileList" width="600px" append-to-body>
      <UploadFile :upload-id="uploadId" :business-code="businessCode" v-if="resumeFileList"></UploadFile>
    </el-dialog>
  </div>
</template>

<script>
import { listSatisfied, getSatisfied, delSatisfied, addSatisfied, updateSatisfied } from "@/api/enterprise/satisfied";
import UploadFile from "../../upload/index.vue";
import {uuid} from "../../../components/Process/common/bpmnUtils";
import FileView from "@/components/FileView/index"
export default {
  name: "Satisfied",
  dicts: ['survey_type'],
  components: {UploadFile,FileView},
  props: {
        // 企业id
        enterpriseId: {
          type: String,
          default: null
        }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      show:false,
      uploadId: null,
      resumeFileList: false,
      typeSelect:0,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户满意度表格数据
      satisfiedList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      businessCode: "satisfied",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        enterpriseId: null,
        surveyType: null,
        investigator: null,
        respondent: null,
        responsiblePerson: null
      },
      // 表单参数
      form: {
        id: null,
        enterpriseId: null,
        surveyType: null,
        investigationTime: null,
        investigator: null,
        respondent: null,
        satisfactionScore: 0,
        correction: null,
        responsiblePerson: null,
        closeTime: null,
        remark: null
      },
      // 表单校验
      rules: {
        surveyType: [
          { required: true, message: "调查分类不能为空", trigger: "change" }
        ],
        investigationTime: [
          { required: true, message: "调查时间不能为空", trigger: "blur" }
        ],
        investigator: [
          { required: true, message: "调查人不能为空", trigger: "blur" }
        ],
        respondent: [
          { required: true, message: "被调查人不能为空", trigger: "blur" }
        ],
        satisfactionScore: [
          { required: true, message: "满意度评分不能为空", trigger: "change" }
        ],
        responsiblePerson: [
          { required: true, message: "责任人不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.queryParams.enterpriseId = this.enterpriseId;
    this.getList();
  },
  methods: {
    /** 查询客户满意度列表 */
    getList() {
      this.loading = true;
      listSatisfied(this.queryParams).then(response => {
        this.satisfiedList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.show=false;
      this.typeSelect=0;
      this.reset();
    },
    handlePictureCardPreviewResume(id) {
      this.uploadId = id;
      this.resumeFileList = true;
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        enterpriseId: null,
        surveyType: null,
        investigationTime: null,
        investigator: null,
        respondent: null,
        satisfactionScore: 0,
        correction: null,
        responsiblePerson: null,
        closeTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.enterpriseId = this.enterpriseId;
      this.form.id=uuid();
      this.show=true;
      this.open = true;
      this.title = "添加客户满意度";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSatisfied(id).then(response => {
        this.form = response.data;
        this.show=true;
        this.typeSelect=1;
        this.open = true;
        this.title = "修改客户满意度";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.createTime != null) {
            updateSatisfied(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.show=false;
              this.open = false;
              this.getList();
            });
          } else {
            addSatisfied(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.show=false;
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除客户满意度编号为"' + ids + '"的数据项？').then(function() {
        return delSatisfied(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('blrs/satisfied/export', {
        ...this.queryParams
      }, `satisfied_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
